package com.threesome.memos.dialog;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import com.threesome.memos.R;
import com.threesome.memos.wheel.NumericWheelAdapter;
import com.threesome.memos.wheel.OnWheelChangedListener;
import com.threesome.memos.wheel.WheelView;

public class TimePicker extends Activity implements OnWheelChangedListener, OnClickListener {
	public static final String HOUR = "hour";
	public static final String MINUTE = "minute";
	private WheelView mHourWheel;
	private WheelView mMinuteWheel;
	private int mHour;
	private int mMinute;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_time_picker);
		
		mHourWheel = (WheelView)findViewById(R.id.hour_wheel);
		mMinuteWheel = (WheelView)findViewById(R.id.minute_wheel);
		
		Calendar calendar = Calendar.getInstance();
		mHour = getIntent().getIntExtra(HOUR, calendar.get(Calendar.HOUR_OF_DAY));
		mMinute = getIntent().getIntExtra(MINUTE, calendar.get(Calendar.MINUTE));
		
		mHourWheel.setAdapter(new NumericWheelAdapter(0, 23, "00"));
		mHourWheel.setCyclic(true);
		mHourWheel.setCurrentItem(mHour);
		mHourWheel.setLabel(getString(R.string.hour));
		mHourWheel.TEXT_SIZE = 18;
		
		mMinuteWheel.setAdapter(new NumericWheelAdapter(0, 59, "00"));
		mMinuteWheel.setCyclic(true);
		mMinuteWheel.setCurrentItem(mMinute);
		mMinuteWheel.setLabel(getString(R.string.minute));
		mMinuteWheel.TEXT_SIZE = 18;
		
		mHourWheel.addChangingListener(this);
		mMinuteWheel.addChangingListener(this);
		findViewById(R.id.save).setOnClickListener(this);
		findViewById(R.id.cancel).setOnClickListener(this);
	}



	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		switch (wheel.getId()) {
		case R.id.hour_wheel:
			mHour = newValue;
			break;
		case R.id.minute_wheel:
			mMinute = newValue;
			break;
		default:
			break;
		}
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.cancel:
			finish();
			break;
		case R.id.save:
			Intent intent = new Intent();
			intent.putExtra(HOUR, mHour);
			intent.putExtra(MINUTE, mMinute);
			setResult(Activity.RESULT_OK, intent);
			finish();
			break;
		default:
			break;
		}
	}
}
